package com.cy.ppj.web.admin.banner;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.Result;
import com.cy.ppj.model.ao.banner.BannerAddAO;
import com.cy.ppj.model.ao.banner.BannerEditAO;
import com.cy.ppj.model.domian.banner.Banner;
import com.cy.ppj.model.enums.CommonStatus;
import com.cy.ppj.model.enums.banner.BannerType;
import com.cy.ppj.model.query.banner.BannerAdminQuery;
import com.cy.ppj.model.vo.BannerTypeVO;
import com.cy.ppj.service.banner.BannerService;
import com.cy.ppj.service.comm.FileService;
import com.cy.ppj.web.BaseController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * 后台-banner管理
 *
 * @author: tanyilin
 * @create: 2018年9月20日 下午6:11:58
 */
@Slf4j
@RestController
@RequestMapping("/admin/banner")
@Api(tags = "后台-banner管理")
public class AdminBannerController extends BaseController{

	@Autowired
	private BannerService bannerService;
	@Autowired
    private FileService fileService;
	
	@ApiOperation("新增banner")
	@PostMapping("/do/add")
	public Result addBanner(@Validated BannerAddAO bannerAddAO, BindingResult bindingResult) {
		
		this.checkError(bindingResult);
		Banner banner = new Banner();
		BeanUtils.copyProperties(bannerAddAO, banner);
		bannerService.addBanner(banner);
		return ok();
	}
	
	@ApiOperation("编辑banner")
	@PostMapping("/do/edit")
	public Result editBanner(@Validated BannerEditAO bannerEditAO, BindingResult bindingResult) {
		
		this.checkError(bindingResult);
		Banner banner = new Banner();
		BeanUtils.copyProperties(bannerEditAO, banner);
		bannerService.updateBanner(banner);
		return ok();
	}
	
	@ApiOperation("修改banner状态（上下架）")
	@PostMapping("/update/status")
	public Result updateStatus(@ApiParam(value = "bannerId", example = "0", required = true) @RequestParam Long bannerId, 
			@ApiParam(value = "状态", required = true) @RequestParam CommonStatus status) {
		Banner banner = new Banner();
		banner.setId(bannerId);
		banner.setStatus(status);
		bannerService.updateBanner(banner);
		return ok();
	}
	
	@ApiOperation("banner类型列表")
	@GetMapping("/get/type/list")
	public Result<List<BannerTypeVO>> getTypeList() {
		return ok("获取成功", BannerType.getBannerTypeList());
	}
	
	@ApiOperation("banner列表")
	@GetMapping("/get/list")
	public PageBean<Banner> list(BannerAdminQuery query) {
		PageBean<Banner> pageBean = bannerService.selectBannerAdmin(query);
		return pageBean;
	}
	
	@ApiOperation("banner详情")
	@GetMapping("/detail/{id}")
	public Result<Banner> detail(@PathVariable Long id) {
		Banner banner = bannerService.selectBannerDetail(id);
		return ok("", banner);
	}
	
}
